package com.cxd.web.backend;

import com.cxd.entity.EbComment;
import com.cxd.entity.EbUser;
import com.cxd.service.IEbCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.sql.ResultSet;
import java.util.List;
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private IEbCommentService iEbCommentService;
    @RequestMapping("/list")
    public String list(HttpSession session, Model model, @RequestParam(value="page",defaultValue = "1")int psize, @RequestParam(value="pageSize",defaultValue = "3")int pageSize){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        List<EbComment>ebComments=iEbCommentService.listByPage(psize,3);
        final Integer totalPage=iEbCommentService.findTotalPage(pageSize);
        model.addAttribute("ebcomments",ebComments);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currPage",psize);
        System.out.println("EbCommentController==分页数据wei："+ebComments);
        System.out.println(model);
        model.addAttribute("tag",5);
        return "manage/guestbook";
    }
//
//    @RequestMapping("/save")
//    public String save(EbComment ebComment,Model model){
//        System.out.println("ebComment=="+ebComment);
//        Integer result = iEbCommentService.save(ebComment);
//        model.addAttribute("tag",5);
//        if(result<1){
//            return null;
//        }
//        return "manage/manage-result";
//    }

    @GetMapping("/delete")
    public String delete(HttpSession session,Model model,Integer ecId){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        int num=iEbCommentService.delete(ecId);
        model.addAttribute("result",num);
        model.addAttribute("tag",5);
        return "manage/manage-result";
    }

    @RequestMapping("/to_edit")
    public String toEdit(HttpSession session,Model model,Integer ecId){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        EbComment ebComment=iEbCommentService.selectByCondition(ecId);
        model.addAttribute("tag",5);
        model.addAttribute("ebComment",ebComment);
        return "manage/guestbook-modify";
    }

    @RequestMapping("/edit")
    public  String edit(HttpSession session,EbComment ebComment,Model model){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
//        ebComment.setEcReplyTime();
        System.out.println(ebComment);
        int result=iEbCommentService.updateById(ebComment);
        model.addAttribute("result",result);
        model.addAttribute("tag",5);
        return "manage/manage-result";
    }
}
